home *** CD-ROM | disk | FTP | other *** search
- /* sortio2.c - I/O finctions for MERGE2 program */
- /* do_open , d0_close , getrec , putrec */
- #include "stdio.h"
- #include "cminor.h"
- #include "sortspec.h"
-
- extern SORTSPEC sspec ; /* has function address and */
- /* other sort spec. info */
- FILE *gfopen() ;
-
- FILE *do_open(fname,fmode) /* open file & check for errors */
- char fname[] ; /* file name */
- char fmode[] ; /* read/write/append mode */
- { /* return a file pointer */
- FILE *fd ;
-
- fd = gfopen(fname,fmode,sspec.ftype) ;
- if( fd == NULL )
- { printf("\n can't open file - %s \n",fname) ;
- exit( 8 ) ;
- }
- return( fd ) ;
- }
-
-
- int do_close(fd,fname) /* close file & check for errors */
- FILE *fd ; /* file pointer */
- char fname[] ; /* name of file being closed */
- {
- if( fclose(fd) < 0 )
- { printf("\n can't close file - %s \n",fname) ;
- exit( 10 ) ;
- }
- }
-
-
- int getrec(rec,maxr,fd) /* get a record */
- char rec[] ; /* put it here in string form */
- int maxr ; /* maximum length permitted */
- FILE *fd ; /* file pointer for input file */
- { /* returns no. chars used in rec */
- return( (*sspec.pget)(rec,maxr,fd)) ; /* use name get fun. */
- }
-
-
- int putrec(rec,fd) /* output one record */
- char rec[] ; /* put it here in string form */
- FILE *fd ; /* output file pointer */
- {
- return( (*sspec.pput)(rec,fd) ) ; /* use specified put fun. */
- }
-
-
- /* ******************************* line oriented functions **** */
-
- int getl(s,maxs,fd) /* get one line from input file */
- char s[] ; /* put it here in string form */
- int maxs ; /* maximum length permitted */
- FILE *fd ; /* file pointer for input file */
- { /* getl returns no. chars used in s */
- /* or -1 if EOF reached */
- int i ;
- /* get next line input */
- if( fgets(s,maxs-1,fd) == NULL )
- return( -1 ) ; /* EOF - return special length value */
- i = strlen(s) ; /* get string length */
- if( s[i-1] != '\n' ) /* see if a new-line is present */
- { s[i] = '\n' ; /* n - append one */
- s[i+1] = '\0' ; /* restore end-of-string marker */
- i = i + 1 ; /* sdjust string length */
- }
- /* return length of line */
- return(i+1) ; /* count the '\0' at end too */
- }
-
-
- int putl(s,fd) /* output one line of text */
- char s[] ; /* line to output in string form */
- FILE *fd ; /* output file pointer */
- {
- return( fputs(s,fd) ) ; /* use fputs library function */
- }
-
-
- /* ******************************* fixed length record functions*/
-
- #define BYTE_SIZE 1 /* fread/fwrite element size */
-
- int getr(rec,maxr,fd) /* get a fixed length record */
- char rec[] ; /* the record is here */
- int maxr ; /* dummy size variable */
- FILE *fd ; /* file stream pointer */
- {
- int nr ; /* number of bytes to read */
-
- nr = fread(rec,BYTE_SIZE,sspec.rec_size,fd) ;
- /* check for partial record */
- if( (nr > 0 ) && (nr < sspec.rec_size) )
- { /* partial record */
- printf("\n partial record at %1d \n",ftell(fd)) ;
- exit( 2 ) ;
- }
- return( nr ) ;
- }
-
-
- int putr(rec,fd) /* output a fixed length record */
- char rec[] ; /* store the record here */
- FILE *fd ; /* file streem pointer */
- {
- int nw ;
-
- nw = fwrire(rec,BYTE_SIZE,sspec.rec_size,fd) ;
- /* check that entire record is written */
- if( nw != sspec.rec_size )
- { /* output problem */
- printf("\n write error at %1d \n",ftell(fd)) ;
- exit( 3 ) ;
- }
- return( nw ) ;
- }
-
-
-
-